package org.nightrunner.d3.entity;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.OrderBy;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;

/**
 * 
 * 装备分类
 * 
 * @author NightRunner
 * @Date 2012-7-26
 * @version 1.0
 */
@Entity
@Indexed
@Table(catalog = "d3", name = "d3_category")
public class Category implements java.io.Serializable {

	private static final long serialVersionUID = -1236421062551602669L;

	/**
	 *分类ID
	 */
	@DocumentId
	private String categoryId;

	/**
	 *分类名称
	 */
	@Field(index = Index.TOKENIZED, store = Store.YES, name = "categoryName")
	private String categoryName;

	/**
	 * 描述
	 */
	@Field(index = Index.TOKENIZED, store = Store.YES, name = "categoryDescription")
	private String categoryDescription;

	/**
	 * 排序字段
	 */
	private Integer sort = 0;

	/**
	 * 父类别
	 */
	private Category superCategory;

	/**
	 * 子类别
	 */
	private List<Category> subCategorys;

	@Column(name = "sort")
	public Integer getSort() {
		return sort;
	}

	/*
	 * @OneToMany(fetch = FetchType.LAZY, targetEntity = Category.class, cascade
	 * = CascadeType.ALL, mappedBy = "superCategory")
	 * 
	 * @OrderBy(clause = "sort ASC") public List<Category> getSubCategorys() {
	 * return subCategorys; }
	 */
	@Id
	@GeneratedValue(generator = "paymentableGenerator")
	@GenericGenerator(name = "paymentableGenerator", strategy = "uuid")
	@Column(name = "category_id", length = 32)
	public String getCategoryId() {
		return this.categoryId;
	}

	@Column(name = "category_name", length = 200)
	@Field(index = Index.TOKENIZED, name = "categoryName", store = Store.YES)
	public String getCategoryName() {
		return this.categoryName;
	}

	@Column(name = "category_description", length = 200)
	public String getCategoryDescription() {
		return this.categoryDescription;
	}

	@ManyToOne(optional = true)
	@JoinColumn(name = "super_category_id", referencedColumnName = "category_id")
	public Category getSuperCategory() {
		return this.superCategory;
	}

	private List<CTE> CTEs;

	@OneToMany(targetEntity = CTE.class, cascade = CascadeType.ALL, mappedBy = "category", fetch = FetchType.EAGER)
	@OrderBy(clause = "sort ASC")
	public List<CTE> getCTEs() {
		return CTEs;
	}

	@OneToMany(fetch = FetchType.LAZY, targetEntity = Category.class, cascade = CascadeType.ALL, mappedBy = "superCategory")
	@OrderBy(clause = "sort ASC")
	public List<Category> getSubCategorys() {
		return subCategorys;
	}

	/** default constructor */
	public Category() {
	}

	/** minimal constructor */
	public Category(String categoryId) {
		this.categoryId = categoryId;
	}

	public void setSuperCategory(Category superCategory) {
		this.superCategory = superCategory;
	}

	public void setCategoryDescription(String categoryDescription) {
		this.categoryDescription = categoryDescription;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}

	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}

	public void setSubCategorys(List<Category> subCategorys) {
		this.subCategorys = subCategorys;
	}

	@Override
	public String toString() {
		return getCategoryName();
	}

	public void setCTEs(List<CTE> ctes) {
		this.CTEs = ctes;
	}
}